package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import models.Assignment;
import models.Course;
import models.CourseRequest;
import models.Student;
import models.Submission;
import models.Tutorial;
import others.DumbFS;
import play.mvc.Controller;
import play.mvc.With;

@With (Secure.class)
public class Ajax extends Controller {

	@Check ("ta")
	public static void getAssignments(long courseId) {
		Course course = Course.findById(courseId);
		ArrayList<Assignment.Object> assignments = new ArrayList<Assignment.Object>();
		for (Assignment assignment : course.assignments) {
			assignments.add(new Assignment.Object(assignment.id, assignment.title, assignment.deadline));
		}
		renderJSON(assignments);
	}

	@Check ("ta")
	public static void getSubmissions(long assignmentId) throws ZipException, IOException {
		Assignment assignment = Assignment.findById(assignmentId);
		ArrayList<Submission.Object> submissions = new ArrayList<Submission.Object>();
		ArrayList<Student.Object> students = new ArrayList<Student.Object>();
		for (Submission submission : assignment.submissions) {
			Student.Object student = null;
			for (Student.Object stuObj : students) {
				if (stuObj.username.equals(submission.student.username)) {
					student = stuObj;
					break;
				}
			}
			if (student == null) {
				student = new Student.Object(submission.student.id, submission.student.username);
				students.add(student);
			}

			ArrayList<String> files = new ArrayList<String>();

			ZipFile submissionFile = new ZipFile(DumbFS.getInstance().getSubmission(submission));
			Enumeration entries = submissionFile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				if (entry.isDirectory())
					continue;
				files.add(entry.getName());
			}

			submissions.add(new Submission.Object(submission.id, submission.grade, student, files));
		}

		renderJSON(submissions);
	}

	@Check ("ta")
	public static void addTutorials(long courseId, String tutorials) {
		Course course = Course.findById(courseId);
		String[] tuts = tutorials.split(",");
		for (String tut : tuts) {
			if (Tutorial.find("byCourseAndNumber", course, tut.trim()).first() != null)
				continue;
			new Tutorial(course, tut.trim()).save();
		}
		flash.success("Tutorials added successfully");
	}

	public static void joinCourse(long tutorialId) {
		Student st = Security.getStudent();
		Tutorial tut = Tutorial.findById(tutorialId);
		List<CourseRequest> reqs = CourseRequest.find("byTutorial.courseAndStudent", tut.course, st).fetch();

		for (CourseRequest req : reqs) {
			req.delete();
		}

		new CourseRequest(tut, st).save();
	}

	public static void pickSubmission(long submissionId) {
		Submission sub = Submission.find("byIdAndStudent", submissionId, Security.getStudent()).first();
		notFoundIfNull(sub);

		List<Submission> subs = Submission.find("byAssignmentAndStudent", sub.assignment, sub.student).fetch();
		for (Submission s : subs) {
			s.toGrade = false;
			s.save();
		}

		sub.toGrade = true;
		sub.save();
	}
}
